package mp.study.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import mp.study.demo.bean.School;
import mp.study.demo.bean.Student;
import mp.study.demo.mapper.SchoolMapper;
import mp.study.demo.mapper.StudentMapper;
import mp.study.demo.service.SchAndStuService;
import mp.study.demo.vo.SchoolAndStudentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/6/20 9:26
 */
@Service
public class SchAndStuServiceImpl implements SchAndStuService
{
    @Autowired
    SchoolMapper schoolMapper;

    @Autowired
    StudentMapper studentMapper;

    //根据id查询数据
    @Override
    public SchoolAndStudentVO getInfoById(Integer id)
    {
        SchoolAndStudentVO schoolAndStudentVO = new SchoolAndStudentVO();
        School school = schoolMapper.selectById(id);
        BeanUtils.copyProperties(school, schoolAndStudentVO);

        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("school_id", id);
        List<Student> students = studentMapper.selectList(wrapper);
        schoolAndStudentVO.setStudents(students);
        return schoolAndStudentVO;
    }

    //直接查询所有数据
    @Override
    public List<SchoolAndStudentVO> getAllInfo()
    {
        List<School> schools = schoolMapper.selectList(null);
        List<SchoolAndStudentVO> list = new ArrayList<>();
        for (School item : schools)
        {
            SchoolAndStudentVO schoolAndStudentVO = new SchoolAndStudentVO();
            QueryWrapper<Student> wrapper = new QueryWrapper<>();
            wrapper.eq("school_id", item.getId());
            List<Student> students = studentMapper.selectList(wrapper);
            BeanUtils.copyProperties(item, schoolAndStudentVO);
            schoolAndStudentVO.setStudents(students);
            list.add(schoolAndStudentVO);
        }
        return list;
    }
}
